package com.starblink.mine.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.apollo.ext.optional
import com.starblink.mine.ui.bean.MineItemEntry
import com.starblink.mine.ui.bean.SubBottomEntry
import com.starblink.mine.ui.bean.SubStoreEntry
import com.starblink.rocketreserver.FetchMySubscribeBrandQuery
import com.starblink.rocketreserver.FetchMySubscribeMerchantWebQuery
import com.starblink.rocketreserver.FetchUserMessageUnreadCountV2Query
import com.starblink.rocketreserver.PageQueryMyCollectProductsQuery
import com.starblink.rocketreserver.type.PageQuery4CollectProductReq
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

class NavMineVM : BaseViewModel() {

    var mList: MutableLiveData<ArrayList<Any>> = MutableLiveData()
    var headData: MutableLiveData<ArrayList<String>?> = MutableLiveData()
    var unreadMessage: MutableLiveData<Boolean> = MutableLiveData() // true表示存在未读消息
    var subWishData: MutableLiveData<ArrayList<Any>> = MutableLiveData()
    var subStoreData: MutableLiveData<ArrayList<Any>> = MutableLiveData(arrayListOf())
    var subBrandData: MutableLiveData<ArrayList<Any>> = MutableLiveData(arrayListOf())

    fun initRefresh() {
        //未登录态
//      checkUserUnreadMessageExist()

        refreshWishlist()

        refreshBrands()

        refreshStores()
    }

    fun refreshWishlist() {
        viewModelScope.launch {
            val subList = SkGqlClient.skQuery(
                PageQueryMyCollectProductsQuery(
                    pageQuery4CollectProduct = PageQuery4CollectProductReq(
                        wishListId = 0,
                        needFilterResultList = Optional.present(false),
                        pageNo = 1.optional(),
                        pageSize = Optional.present(5)
                    )
                ),
                //fetchPolicy = FetchPolicy.NetworkFirst
            )?.data?.pageQueryMyCollectProducts?.productList?.mapNotNull { it.productF }
            subList.let {
                if (!subList.isNullOrEmpty()) {
                    subWishData.value?.clear()
                    if (subList.size < 5) {
                        subWishData.value?.addAll(subList)
                        for (i in subList.size until 5) {
                            subWishData.value?.add("")
                        }
                    } else {
                        subWishData.value?.addAll(subList.subList(0, 5))
                    }
                    subWishData.postValue(subWishData.value)

                } else {
                    subWishData.value?.clear()
                    subWishData.value?.add(1)
                    subWishData.value?.add("")
                    subWishData.value?.add("")
                    subWishData.value?.add("")
                    subWishData.value?.add("")
                    subWishData.postValue(subWishData.value)
                }
            }
        }
    }

    fun refreshBrands() {
        viewModelScope.launch {
            val subList = SkGqlClient.skQuery(//fetchMySubscribeMerchantWeb
                FetchMySubscribeBrandQuery(
                    1,
                    4,
                )
            )?.data?.fetchMySubscribeBrand?.subList?.mapNotNull { it.brandVOF }
            subList?.let {
                subBrandData.value?.clear()
                if (!subList.isNullOrEmpty()) {
                    subBrandData.value?.addAll(subList)
                }
                subBrandData.postValue(subBrandData.value)
            }
        }
    }

    fun refreshStores() {
        viewModelScope.launch {
            val list = SkGqlClient.skQuery(//fetchMySubscribeMerchantWeb
                FetchMySubscribeMerchantWebQuery(
                    1,
                    4,
                )
            )?.data?.fetchMySubscribeMerchantWeb?.subList?.mapNotNull { it.merchantWebVOF }
            list?.let {
                subStoreData.value?.clear()
                if (!list.isNullOrEmpty()) {
                    subStoreData.value?.addAll(list)
                }
                subStoreData.postValue(subStoreData.value)
            }

        }
    }

    /**
     * 检查用户是否存在未读消息
     */
    fun checkUserUnreadMessageExist() {
        viewModelScope.async {
            if (!UserDataCenter.isLogin()) {
                return@async
            }

            val response = SkGqlClient.skQuery(
                FetchUserMessageUnreadCountV2Query(),
            )
            if (response?.data != null && response.data!!.fetchUserMessageUnreadCountV2 != null) {
                unreadMessage.postValue(response.data!!.fetchUserMessageUnreadCountV2.systemMsgCount > 0)
            }
        }
    }

    fun initItemData() {
        mList.value = arrayListOf(
            SubStoreEntry(
                desc = "subStore",
                subData = subWishData
            ),
            SubBottomEntry(
                desc = "subBo",
                subStoreData = subStoreData,
                subBrandData = subBrandData,
            ),
            "My Service",
//            MineItemEntry(
//                iconPath = 101,
//                title = "Invite Friends",
//                type = "1",
//                routeName = ""
//            ),
//            MineItemEntry(
//                iconPath = 102,
//                title = "Become A Creator",
//                type = "1",
//                routeName = ""
//            ),
            MineItemEntry(
                iconPath = 103,
                title = "Account",
                type = "1",
                routeName = ""
            ),
            MineItemEntry(
                iconPath = 104,
                title = "Privacy Settings",
                type = "1",
                routeName = ""
            ),
            MineItemEntry(
                iconPath = 105,
                title = "Feedback",
                type = "1",
                routeName = ""
            )
        )
    }
}